home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15133 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  43 lines

  1. Path: news.polymtl.ca!news
  2. From: Pierre Ferland <pierre@meca.polymtl.ca>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Multiple Inheritance Pointer Problem
  5. Date: Wed, 03 Apr 1996 15:44:58 -0500
  6. Organization: Center for Applied Research on Polymers
  7. Message-ID: <3162E34A.15FB@meca.polymtl.ca>
  8. References: <31622A26.3633@nmaa.org> <s8k9zxo51p.fsf_-_@socrates.ansa.co.uk>
  9. NNTP-Posting-Host: crasp.meca.polymtl.ca
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; AIX 2)
  14.  
  15. Yes, right Toby.  One may also try this:
  16.  
  17. class Base_c { };
  18. class Derived_c : public Base_c {};
  19.   
  20. int main(int, char *[]) {
  21.  
  22.     Derived_c  derived;
  23.  
  24.     Derived_c *ptr_derived = &derived;
  25.     Base_c    *ptr_base    = (Base_c *)ptr_derived;
  26.     void      *ptr_void    = (void *)  ptr_derived;
  27.  
  28.     printf("\n%x %x %x", ptr_derived, ptr_base, ptr_void);
  29.  
  30. }
  31.  
  32. The type cast from Derived_c * to Base_c * changes the address,
  33. so that the pointer actually points inside a Base_c object
  34. (the Base_c part of Derived_c)
  35.  
  36. The type cast to void * obviously leaves the pointer unchanged.
  37. I recommend that you NEVER hard typecast a derived class
  38. into a void (example in a virtual constructor).  Even though
  39. it sounds obvious many beginners do the fatal mistake, and are
  40. brought into a land of bizarre bugs...
  41.  
  42. Pierre
  43.